package main

import (
	"fmt"
	"sync"
)

/*
两个goroutine 两个channel
 生成 0——100的数字发送到ch1
 从ch1取出数据计算平方，将结果发送到ch2

*/
var wg sync.WaitGroup

func main() {
	ch1 := make(chan int, 100)
	wg.Add(2)
	go func() {
		defer wg.Done()
		for i := 0; i < 100; i++ {
			ch1 <- i
		}
		close(ch1)
	}()

	ch2 := make(chan int, 100)
	go func() {
		defer wg.Done()
		for v := range ch1 {
			fmt.Printf("ch2 <- ch1 : %v\n", v)
			ch2 <- v * v
		}
		close(ch2)
	}()

	for v := range ch2 {
		fmt.Printf("main <- ch2 : %v\n", v)
	}
	wg.Wait()
}
